import Api from 'kuan-request'
import router from '@/router'
import { Notification } from 'element-ui'
import authority from './authority'

const api = new Api({
  getHeaders: () => {
    const token = authority.get()
    if (!token) return {}
    return { Authorization: `bear ${token}` }
  },
  handleError: (status, msg, error) => {
    console.log(status, msg, error)
    if (status === 401) router.push({ name: 'login' })
  }, // 自定义错误处理
  // loading: loadingCtrl, // 自定义loading
  toast(message) {
    Notification({
      type: 'warning',
      title: '警告',
      message
    })
  },
  alertDetail: true // 显示详细报错信息
})

export default api.create({})
